﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for mahoa
/// </summary>
public class mahoa
{
    private static string EncryptionKey = "qlnv";
    private static byte[] key = { };
    private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
	public mahoa()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static string Encrypt(string chuoicanmahoa,string pass)
    {
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes(pass);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            Byte[] inputByteArray =Encoding.UTF8.GetBytes(chuoicanmahoa);
            MemoryStream ms =new MemoryStream();
            CryptoStream cs =new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception ex)
        {
            return "";
        }
    }
    public static string Decrypt(string chuoimahoa,string pass)
    {
        Byte[] inputByteArray =new Byte[chuoimahoa.Length];
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes(pass);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(chuoimahoa);
            MemoryStream ms =new MemoryStream();
            CryptoStream cs =new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            Encoding encoding =Encoding.UTF8;
            return encoding.GetString(ms.ToArray());
        }
        catch (Exception ex)
        {
            return "";
        }
    }
    public static string mahoaluong(int luong)
    {
        return mahoa.Encrypt(luong.ToString(), "qlnvluong");
    }
    public static Int32 giaimaluong(string chuoimahoa)
    {
        return Int32.Parse(mahoa.Decrypt(chuoimahoa, "qlnvluong"));
    }
}
